<html>
<head><meta charset="utf-8"><title>Generic consts · t-compiler/const-eval · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/index.html">t-compiler/const-eval</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Generic.20consts.html">Generic consts</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="177992695"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Generic%20consts/near/177992695" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Generic.20consts.html#177992695">(Oct 12 2019 at 15:05)</a>:</h4>
<p><span class="user-mention" data-user-id="124288">@oli</span> I didn't know that we "basically" already have generic consts... look at this:<br>
<a href="https://doc.rust-lang.org/nightly/std/mem/union.MaybeUninit.html#associatedconstant.UNINIT" target="_blank" title="https://doc.rust-lang.org/nightly/std/mem/union.MaybeUninit.html#associatedconstant.UNINIT">https://doc.rust-lang.org/nightly/std/mem/union.MaybeUninit.html#associatedconstant.UNINIT</a></p>



<a name="177992704"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Generic%20consts/near/177992704" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Generic.20consts.html#177992704">(Oct 12 2019 at 15:05)</a>:</h4>
<p>doesn't this throw a wrench into our validation story? We usually validate all <code>const</code> items, but we obviously cannot validate this one</p>



<a name="177992710"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Generic%20consts/near/177992710" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Generic.20consts.html#177992710">(Oct 12 2019 at 15:05)</a>:</h4>
<p>and yet it is promoted etc the same way as all the constants that we <em>did</em> properly check</p>



<a name="177993898"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Generic%20consts/near/177993898" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Generic.20consts.html#177993898">(Oct 12 2019 at 15:35)</a>:</h4>
<p>Yeah associated consts totally entail generic consts with weirder syntax</p>



<a name="177994043"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Generic%20consts/near/177994043" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Generic.20consts.html#177994043">(Oct 12 2019 at 15:39)</a>:</h4>
<p><span aria-label="explosion" class="emoji emoji-1f4a5" role="img" title="explosion">:explosion:</span></p>



<a name="177994047"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Generic%20consts/near/177994047" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Generic.20consts.html#177994047">(Oct 12 2019 at 15:39)</a>:</h4>
<p>those were our plans for const soundness, I think^^</p>



<a name="177994099"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Generic%20consts/near/177994099" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Generic.20consts.html#177994099">(Oct 12 2019 at 15:40)</a>:</h4>
<p>or at least for <em>first</em> looking at soundness and <em>then</em> adding features that make it harder</p>



<a name="177994121"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Generic%20consts/near/177994121" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Generic.20consts.html#177994121">(Oct 12 2019 at 15:41)</a>:</h4>
<p>fwiw we don't have generic statics at least but I'm not sure that helps ^^</p>



<a name="177994127"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Generic%20consts/near/177994127" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Generic.20consts.html#177994127">(Oct 12 2019 at 15:41)</a>:</h4>
<p>lol</p>



<a name="177994131"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Generic%20consts/near/177994131" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Generic.20consts.html#177994131">(Oct 12 2019 at 15:41)</a>:</h4>
<p>what would those even mean</p>



<a name="177994170"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Generic%20consts/near/177994170" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Generic.20consts.html#177994170">(Oct 12 2019 at 15:42)</a>:</h4>
<p>re: having unique but fixed addresses and things</p>



<a name="177994197"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Generic%20consts/near/177994197" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Generic.20consts.html#177994197">(Oct 12 2019 at 15:43)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> I think it means unique address per instantiation of parameters (modulo lifetimes cause erasure)</p>



<a name="177994612"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Generic%20consts/near/177994612" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Generic.20consts.html#177994612">(Oct 12 2019 at 15:52)</a>:</h4>
<p>across all translation units? what about the usual diamond problem?</p>



<a name="177995268"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Generic%20consts/near/177995268" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Generic.20consts.html#177995268">(Oct 12 2019 at 16:08)</a>:</h4>
<p>Well if there were no problems then it would already be a thing. ^^</p>
<p>I believe it wouldn't be unique across cdylibs which afaik holds for normal statics as well. dylib's do throw a wrench into the issue tho (which <span class="user-mention" data-user-id="124289">@rkruppe</span>  is more familiar with)</p>



<a name="177995344"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Generic%20consts/near/177995344" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Generic.20consts.html#177995344">(Oct 12 2019 at 16:10)</a>:</h4>
<p>Yeah dylibs are a dealbreaker for generic statics</p>



<a name="177995573"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Generic%20consts/near/177995573" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Generic.20consts.html#177995573">(Oct 12 2019 at 16:16)</a>:</h4>
<p>I was thinking of the good ol' "crate A defines the static, crate B1 and B2 independently make an instance of it with the same generic param, crate C depends on B1+B2"</p>



<a name="177995576"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Generic%20consts/near/177995576" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Generic.20consts.html#177995576">(Oct 12 2019 at 16:16)</a>:</h4>
<p>Oh that's solvable</p>



<a name="177995577"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Generic%20consts/near/177995577" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Generic.20consts.html#177995577">(Oct 12 2019 at 16:16)</a>:</h4>
<p>looks like each of B1's and B2's LLVM IR would then have the static in it, leading to...</p>



<a name="177995581"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Generic%20consts/near/177995581" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Generic.20consts.html#177995581">(Oct 12 2019 at 16:16)</a>:</h4>
<p>okay? good :D</p>



<a name="177995593"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Generic%20consts/near/177995593" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Generic.20consts.html#177995593">(Oct 12 2019 at 16:17)</a>:</h4>
<p>All non-leaf crates just make a note that they reference that instantiation, compute the symbol name, and leave actually defining it to the leaf crate</p>



<a name="177995599"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Generic%20consts/near/177995599" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Generic.20consts.html#177995599">(Oct 12 2019 at 16:17)</a>:</h4>
<p>The dylib problem is that you can't defer to the leaf crate that way with dylibs</p>



<a name="177995677"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Generic%20consts/near/177995677" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Generic.20consts.html#177995677">(Oct 12 2019 at 16:19)</a>:</h4>
<p>Or should it be root crate? Whatever, the bin/staticlib/cdylib</p>



<a name="177995689"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Generic%20consts/near/177995689" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Generic.20consts.html#177995689">(Oct 12 2019 at 16:19)</a>:</h4>
<p>iirc <span class="user-mention" data-user-id="124287">@mw</span> had plans to kill dylibs or something</p>



<a name="177995740"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Generic%20consts/near/177995740" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Generic.20consts.html#177995740">(Oct 12 2019 at 16:20)</a>:</h4>
<p>Several people (including me) have been loudly wishing for their death for years</p>



<a name="177995745"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Generic%20consts/near/177995745" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Generic.20consts.html#177995745">(Oct 12 2019 at 16:20)</a>:</h4>
<p>yeah but I mean actual plans ^^</p>



<a name="177995747"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Generic%20consts/near/177995747" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Generic.20consts.html#177995747">(Oct 12 2019 at 16:20)</a>:</h4>
<p>or like a roadmap goal</p>



<a name="177995760"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Generic%20consts/near/177995760" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Generic.20consts.html#177995760">(Oct 12 2019 at 16:21)</a>:</h4>
<p>(but I think we digress from the const validation stuff this topic was really about... woops, my bad... Zulip is hard!)</p>



<a name="178072772"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Generic%20consts/near/178072772" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Generic.20consts.html#178072772">(Oct 14 2019 at 04:45)</a>:</h4>
<p>cc <span class="user-mention" data-user-id="118594">@ecstatic-morse</span> <span aria-label="slight smile" class="emoji emoji-1f642" role="img" title="slight smile">:slight_smile:</span></p>



<a name="178072986"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Generic%20consts/near/178072986" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Generic.20consts.html#178072986">(Oct 14 2019 at 04:49)</a>:</h4>
<p>I have read this thread, but I don't <em>really</em> understand all the background here <span aria-label="half frown" class="emoji emoji-1f615" role="img" title="half frown">:half_frown:</span>. Most of my knowledge is about the static side of const validation, but this seems more about the dynamic part? I suppose if <span class="user-mention" data-user-id="120791">@RalfJ</span> needs more feedback he could try to explain it to me, but probably someone else is more capable.</p>



<a name="178136584"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Generic%20consts/near/178136584" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Generic.20consts.html#178136584">(Oct 14 2019 at 20:34)</a>:</h4>
<p><span class="user-mention" data-user-id="118594">@ecstatic-morse</span> for <code>const</code> and <code>static</code>, we actually iterate all of these in the crate you are compiling to test their sanity, even if they are not used</p>



<a name="178136608"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Generic%20consts/near/178136608" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Generic.20consts.html#178136608">(Oct 14 2019 at 20:34)</a>:</h4>
<p>e.g. when you have a lib crate, you may have many consts that only your clients will use; this makes sure we check them</p>



<a name="178136632"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Generic%20consts/near/178136632" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Generic.20consts.html#178136632">(Oct 14 2019 at 20:35)</a>:</h4>
<p>but when they are generic, we can't... we'd have to check <em>every possible</em> instance, with all possible types, and that's infinitely many...</p>



<a name="178138304"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Generic%20consts/near/178138304" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Generic.20consts.html#178138304">(Oct 14 2019 at 20:54)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> Is sanity used to mean "const-safety" here? On the static side, we assume the worst about <code>T</code> w.r.t. its qualifications. This doesn't really apply to MIRI though. Can you give an example where this would be a problem?</p>



<a name="178139016"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Generic%20consts/near/178139016" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Generic.20consts.html#178139016">(Oct 14 2019 at 21:02)</a>:</h4>
<p>yeah it's basically const-safety</p>



<a name="178139024"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Generic%20consts/near/178139024" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Generic.20consts.html#178139024">(Oct 14 2019 at 21:02)</a>:</h4>
<p>it's the <code>validity.rs</code> thing</p>



<a name="178139041"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Generic%20consts/near/178139041" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Generic.20consts.html#178139041">(Oct 14 2019 at 21:02)</a>:</h4>
<p>the biggie in terms of problems is promotion</p>



<a name="178139067"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Generic%20consts/near/178139067" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Generic.20consts.html#178139067">(Oct 14 2019 at 21:03)</a>:</h4>
<p>if we promote <code>STATIC * 2</code> and the static (type <code>u32</code>) is actually the result of a ptr-to-int cast, we have to abort compilation</p>



<a name="178229349"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Generic%20consts/near/178229349" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Generic.20consts.html#178229349">(Oct 15 2019 at 19:57)</a>:</h4>
<p>Ugh sorry. I'm so easily distracted. I still don't understand why this falls down around <strong>generic</strong> consts specifically. Are we worried about the body of a const being const-unsafe for some <code>T</code> but not others? On the static analysis side, we reason about const bodies pre-monomorphization, so it's not quite <span aria-label="boom" class="emoji emoji-1f4a5" role="img" title="boom">:boom:</span> I think? A specific example would help me here.</p>



<a name="178229805"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Generic%20consts/near/178229805" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Generic.20consts.html#178229805">(Oct 15 2019 at 20:02)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span></p>



<a name="178310204"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Generic%20consts/near/178310204" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Generic.20consts.html#178310204">(Oct 16 2019 at 17:29)</a>:</h4>
<blockquote>
<p>why this falls down around generic consts specifically</p>
</blockquote>
<p>consider</p>
<div class="codehilite"><pre><span></span><span class="k">const</span><span class="w"> </span><span class="n">FOO</span>: <span class="kt">bool</span> <span class="o">=</span><span class="w"> </span><span class="p">{...}</span><span class="w"></span>
<span class="k">const</span><span class="w"> </span><span class="n">BAR</span><span class="o">&lt;</span><span class="n">T</span>: <span class="nc">Trait</span><span class="o">&gt;</span>: <span class="kt">bool</span> <span class="o">=</span><span class="w"> </span><span class="p">{...}</span><span class="w"></span>
</pre></div>


<p>if we want to check that <code>FOO</code> is a proper <code>bool</code> that's easy: just run the code, and check if the result is 0x00 or 0x01.<br>
if we want to check that <code>BAR</code> is a proper <code>bool</code>... we can't as we'll have to try <em>every possible type <code>T</code></em>.</p>



<a name="178310575"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Generic%20consts/near/178310575" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Generic.20consts.html#178310575">(Oct 16 2019 at 17:33)</a>:</h4>
<p>Right, but the issue is that we are unable to do dynamic checks in this case for libraries correct? Not that static validation allows you to write a const-unsafe generic initializer today?</p>



<a name="178310713"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Generic%20consts/near/178310713" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Generic.20consts.html#178310713">(Oct 16 2019 at 17:35)</a>:</h4>
<p>with unsafe code, static checks will never be <em>guaranteeing</em> anything</p>



<a name="178310740"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Generic%20consts/near/178310740" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Generic.20consts.html#178310740">(Oct 16 2019 at 17:35)</a>:</h4>
<p>and const allowing unsafe is not a bug, IMO</p>



<a name="178311319"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Generic%20consts/near/178311319" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Generic.20consts.html#178311319">(Oct 16 2019 at 17:41)</a>:</h4>
<p>I'm just trying to establish whether there's currently a soundness bug, since AFAICT all unsafe operations are forbidden in const contexts. I understand that future plans for allowing unsafe code are greatly complicated by this.</p>



<a name="178311616"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Generic%20consts/near/178311616" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Generic.20consts.html#178311616">(Oct 16 2019 at 17:44)</a>:</h4>
<p><span class="user-mention" data-user-id="118594">@ecstatic-morse</span> unconst operations are forbidden but not unsafe ones</p>



<a name="178311658"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Generic%20consts/near/178311658" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Generic.20consts.html#178311658">(Oct 16 2019 at 17:45)</a>:</h4>
<p>e.g. <a href="https://doc.rust-lang.org/nightly/std/ptr/struct.NonNull.html#method.new_unchecked" target="_blank" title="https://doc.rust-lang.org/nightly/std/ptr/struct.NonNull.html#method.new_unchecked">https://doc.rust-lang.org/nightly/std/ptr/struct.NonNull.html#method.new_unchecked</a></p>



<a name="178311823"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Generic%20consts/near/178311823" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Generic.20consts.html#178311823">(Oct 16 2019 at 17:47)</a>:</h4>
<p>Ah okay.</p>



<a name="178311987"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Generic%20consts/near/178311987" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Generic.20consts.html#178311987">(Oct 16 2019 at 17:49)</a>:</h4>
<p>I mean the safety of that particular example doesn't depend on <code>T</code>, but it shouldn't be too hard to come up with an example where it does.</p>



<a name="178312124"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Generic%20consts/near/178312124" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Generic.20consts.html#178312124">(Oct 16 2019 at 17:50)</a>:</h4>
<p>in any case it doesn't execute until it gets monomorphized downstream</p>



<a name="178312929"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Generic%20consts/near/178312929" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Generic.20consts.html#178312929">(Oct 16 2019 at 17:58)</a>:</h4>
<p>I dont know of a soundness bug. but there could be promotion issues.</p>



<a name="178312966"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Generic%20consts/near/178312966" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Generic.20consts.html#178312966">(Oct 16 2019 at 17:59)</a>:</h4>
<p>I should probably try to construct an example... so how does that assoc const stuff work?^^</p>



<a name="178313256"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Generic%20consts/near/178313256" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Generic.20consts.html#178313256">(Oct 16 2019 at 18:01)</a>:</h4>
<div class="codehilite"><pre><span></span><span class="c1">// Crate 1</span>
<span class="k">struct</span> <span class="nc">S</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">(</span><span class="n">T</span><span class="p">);</span><span class="w"></span>

<span class="k">impl</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="w"> </span><span class="n">S</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="c1">// The crate with this code has no warning.</span>
<span class="w">    </span><span class="k">const</span><span class="w"> </span><span class="n">C</span>: <span class="kt">bool</span> <span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="k">union</span> <span class="nc">Transmute</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">i</span>: <span class="kt">u8</span><span class="p">,</span><span class="w"> </span><span class="n">o</span>: <span class="kt">bool</span> <span class="p">}</span><span class="w"></span>
<span class="w">        </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">Transmute</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">i</span>: <span class="mi">5</span><span class="w"> </span><span class="p">}.</span><span class="n">o</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="p">};</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="c1">// Crate 2</span>
<span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">_x</span>: <span class="kp">&amp;</span><span class="nb">&#39;static</span><span class="w"> </span><span class="kt">bool</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="n">S</span>::<span class="o">&lt;</span><span class="kt">i32</span><span class="o">&gt;</span>::<span class="n">C</span><span class="p">;</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>



<a name="178313278"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Generic%20consts/near/178313278" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Generic.20consts.html#178313278">(Oct 16 2019 at 18:01)</a>:</h4>
<p>so this doesnt actually end up quite as bad as I thought; even if we didnt promote this code would still error</p>



<a name="178313309"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Generic%20consts/near/178313309" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Generic.20consts.html#178313309">(Oct 16 2019 at 18:02)</a>:</h4>
<p>because <em>then</em> we do the validity check</p>



<a name="178313379"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Generic%20consts/near/178313379" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Generic.20consts.html#178313379">(Oct 16 2019 at 18:02)</a>:</h4>
<p>so in some sense the behavior is consistent by aggressively failing early... not sure if that's the best strategy^^</p>



<a name="178314240"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Generic%20consts/near/178314240" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Generic.20consts.html#178314240">(Oct 16 2019 at 18:10)</a>:</h4>
<div class="codehilite"><pre><span></span><span class="c1">// crate 1</span>

<span class="k">struct</span> <span class="nc">Generic</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">(</span><span class="n">T</span><span class="p">);</span><span class="w"></span>
<span class="k">union</span> <span class="nc">Transmuter</span><span class="o">&lt;</span><span class="n">T</span>: <span class="nb">Copy</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">t</span>: <span class="nc">T</span><span class="p">,</span><span class="w"> </span><span class="n">byte</span>: <span class="kt">u8</span> <span class="p">}</span><span class="w"></span>

<span class="k">impl</span><span class="o">&lt;</span><span class="n">T</span>: <span class="nb">Copy</span><span class="o">&gt;</span><span class="w"> </span><span class="n">Generic</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">const</span><span class="w"> </span><span class="n">FIVE</span>: <span class="nc">T</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">Transmuter</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">byte</span>: <span class="mi">5</span><span class="w"> </span><span class="p">}.</span><span class="n">t</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="p">};</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="c1">// crate 2</span>

<span class="k">fn</span> <span class="nf">promote_unconst</span><span class="o">&lt;</span><span class="n">T</span>: <span class="nb">Copy</span><span class="o">&gt;</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="kp">&amp;</span><span class="nb">&#39;static</span><span class="w"> </span><span class="n">T</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="o">&amp;</span><span class="n">Generic</span>::<span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span>::<span class="n">FIVE</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>



<a name="178314320"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Generic%20consts/near/178314320" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Generic.20consts.html#178314320">(Oct 16 2019 at 18:11)</a>:</h4>
<p>Is this any worse? It's really just another layer of monomorphization.</p>



<a name="178314704"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Generic%20consts/near/178314704" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Generic.20consts.html#178314704">(Oct 16 2019 at 18:14)</a>:</h4>
<p>mostly it's another layer of "thunking"</p>



<a name="178314710"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Generic%20consts/near/178314710" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Generic.20consts.html#178314710">(Oct 16 2019 at 18:14)</a>:</h4>
<p>or laziness</p>



<a name="178314726"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Generic%20consts/near/178314726" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Generic.20consts.html#178314726">(Oct 16 2019 at 18:14)</a>:</h4>
<p>normal <code>const</code> we check eagerly: all const ever defined are checked</p>



<a name="178314743"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Generic%20consts/near/178314743" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Generic.20consts.html#178314743">(Oct 16 2019 at 18:15)</a>:</h4>
<p>generic const we can only check lazily: only when a const gets used, we check it</p>



<a name="178377222"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Generic%20consts/near/178377222" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Generic.20consts.html#178377222">(Oct 17 2019 at 12:47)</a>:</h4>
<p>these are the kind of post monomorphization errors we can't really eliminate</p>



<a name="178377285"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Generic%20consts/near/178377285" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Generic.20consts.html#178377285">(Oct 17 2019 at 12:48)</a>:</h4>
<p>similar ones have been around since 1.0</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>